package com.expression.uartagent.core;

import android.content.Context;

import com.upcontrol.uartagent.core.inter.DataReceive;
import com.usb2uartagent.IUARTConnectorHelper;
import com.usb2uartagent.Usb2UartagentManager;

/**
 * Copyright (c) 2017, SuZhou CsjBot. All Rights Reserved.
 * www.csjbot.com
 * <p>
 * Created by 浦耀宗 at 2017/02/09 0009-14:06.
 * Email: puyz@csjbot.com
 */
public class UsbUartExpressionHelper implements IUARTConnectorHelper, Usb2UartagentManager.DataHandler
{
    private static boolean bHandleMsgOk;

    @Override
    public void onDataRecevie(byte[] data)
    {
        //针对表情只发送数据，不接受数据，为空
    }

    @Override
    public int init(Context context, String device, int speed)
    {
        return Usb2UartagentManager.getInstance().detectUartIndex(Usb2UartagentManager.EXPRESSION_TYPE, this, speed);
    }

    @Override
    public int send(byte[] data)
    {
        return Usb2UartagentManager.getInstance().sendData(Usb2UartagentManager.EXPRESSION_TYPE, data);
    }

    @Override
    public void destroy()
    {
        Usb2UartagentManager.getInstance().releaseByType(Usb2UartagentManager.EXPRESSION_TYPE);
    }

    @Override
    public void setDataReceive(DataReceive receive) {

    }

    @Override
    public void handleData(byte[] data, int length)
    {
        //验证串口是否是连接投影仪
        if (data != null)
        {
            for (byte code : data)
            {
                if (code != (byte) 0xA8)
                {
                    bHandleMsgOk = false;
                    return;
                }
            }
            bHandleMsgOk = true;
        }

        onDataRecevie(data);
    }

    public static boolean isbHandleMsgOk() {
        return bHandleMsgOk;
    }
}
